package com.dbritos.labusers.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class GenericDaoHibernate<T, PK extends Serializable> implements GenericDao<T, PK> {
    protected final Log log = LogFactory.getLog(getClass());
    private Class<T> persistentClass;
    //private HibernateTemplate hibernateTemplate;
    //private HibernateUtil hibernateUtil;
    //private SessionFactory sessionFactory;
    private Session session;
    private Transaction tx;    
    /**
     * Constructor that takes in a class to see which type of entity to persist.
     * Use this constructor when subclassing.
     *
     * @param persistentClass the class type you'd like to persist
     */
    public GenericDaoHibernate(final Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }

    /**
     * Constructor that takes in a class and sessionFactory for easy creation of DAO.
     *
     * @param persistentClass the class type you'd like to persist
     * @param sessionFactory  the pre-configured Hibernate SessionFactory
     */
    public GenericDaoHibernate(final Class<T> persistentClass, SessionFactory sessionFactory) {
        this.persistentClass = persistentClass;
        //this.sessionFactory = sessionFactory;
        //this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
/*
    public HibernateTemplate getHibernateTemplate() {
        return this.hibernateTemplate;
    }

    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    @Autowired
    @Required
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        //this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
*/
    public List<T> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> getAllDistinct() {
		// TODO Auto-generated method stub
		return null;
	}

	public T get(PK id) {
		// TODO Auto-generated method stub
        session = HibernateFactory.openSession();
		return (T)session.get(persistentClass, id);
	}

	public boolean exists(PK id) {
		// TODO Auto-generated method stub
		return false;
	}

	public T save(T object) {
		// TODO Auto-generated method stub
		startOperation();
		session.saveOrUpdate(object);
		tx.commit();
		return object;
	}

	public void remove(PK id) {
		// TODO Auto-generated method stub
		startOperation();
		session.delete(get(id));
		tx.commit();		
	}

	public List<T> findByNamedQuery(String queryName,
			Map<String, Object> queryParams) {
		// TODO Auto-generated method stub
		return null;
	}

    private void startOperation() throws HibernateException {
        session = HibernateFactory.openSession();
        tx = session.beginTransaction();
    }
}
